package com.huawei.esdk.uc.sametime;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.huawei.esdk.uc.sametime.util.PluginUtil;

public class DllLoader
{

    private static Logger logger = Logger.getLogger(DllLoader.class);

    private Properties properties;

    public DllLoader()
    {
        super();
        properties = new Properties();
    }

    public void loadPropertyConfig()
    {
        String fileName = PluginUtil.getLog4jPath() + File.separator
                + "loadSequence.properties";
        InputStream inputStream = null;
        try
        {
            inputStream = new FileInputStream(fileName);
            properties.load(inputStream);
        }
        catch (FileNotFoundException exception)
        {
            logger.error(exception);
        }
        catch (IOException exception)
        {
            logger.error(exception);
        }
        finally
        {
            if (null != inputStream)
            {
                try
                {
                    inputStream.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                finally
                {
                    inputStream = null;
                }
            }
        }
    }

    public void sequenceLoadDll()
    {
        int dllsum = Integer.parseInt(properties.getProperty("dllsum"));
        String dllPath = PluginUtil.getLog4jPath();
        String dllAbsoluteName = null;
        String dllfile = null;
        for (int i = 1; i <= dllsum; i++)
        {
            dllfile = properties.getProperty("dll" + i);
            dllAbsoluteName = dllPath + File.separator + dllfile;
            logger.debug("load dll " + dllAbsoluteName);
            System.load(dllAbsoluteName);
        }
    }

    public void clear()
    {
        properties.clear();
    }

}
